### Loading in the Data and Libraries ##
library(dplyr)

setwd("")
data<-read.csv("01_Messy Racial Shift Replication.csv")

### Remove People Who Did Not Consent and Who Requested Their Data Be Removed ###
### Does not Need to Be Run on The Dataset Since Drops Are Already Excluded to Protect Participant Privacy ###

#data<-data%>%
#  filter(Consent== "I agree" & remove_data != "Yes")

### Selecting Only Necessary Variables from Qualtrics Provided Variables ###
data<-data%>%
  dplyr:: select(-c(StartDate, EndDate, Status, IPAddress, Progress, Duration..in.seconds., Finished, RecordedDate,
            RecipientLastName, RecipientFirstName, RecipientEmail, ExternalReference, LocationLatitude,
            LocationLongitude, DistributionChannel, UserLanguage, Q_RecaptchaScore, Consent, FTs_DO,
            check_culture_DO, check_political_DO, check_pop_DO, check_geo_DO, RT_DO, ST_DO, SC_DO,
            FL_35_DO, FL_34_DO, Ideology_DO, Authoritarianism_DO, trends_closed_DO))%>%
  rename(Trump_FT=FTs_1, Biden_FT=FTs_2, Swift_FT=FTs_3, Kimmel_FT=FTs_4, Asian_FT=GroupFTs_1, 
         Black_FT=GroupFTs_2, Latinos_FT=GroupFTs_3, White_FT=GroupFTs_4, Alt_Right_FT=GroupFTs_5, BLM_FT=GroupFTs_6,
         Legacy_Admit=Policies_1, Expand_Immi=Policies_2, Inc_Border_Pat=Policies_3, Inc_Tax_Wealthy=Policies_4, 
         No_Trans_Bathroom=Policies_5, Abortion_Access=Policies_6, Sign_Racist_Cand=Participation_1,
         White_History_SchoolBoard=Participation_2, Vote_Antiracist=Participation_3, Rally_for_Inc_Immi=Participation_4, 
         Buy_Poor_Whites_Book=Participation_5, Boycott_Undoc_Immi_Labor=Participation_6, 
         Buy_POC_Book=Participation_7, Buy_Help_Undoc_Immi= Participation_8, Treatment= Treatment_DO)

### Recode Treatment Variable ###
data$Treatment<-case_when(
  data$Treatment== "Timer|pop_shift" ~ "Population Shift",
  data$Treatment== "Timer|pol_shift" ~ "Political Shift",
  data$Treatment== "Timer|culture_shift" ~ "Cultural Shift",
  data$Treatment== "Timer|geo_mobility" ~ "Control"
)

### Rename Timer ###
data<-data%>%
  mutate(treatment_time= Timer_Page.Submit)


### Recode Manipulation Checks ###
data$mc_correct<-case_when(
  data$check_political== "Racial minorities will be the majority of voters by the year 2064." ~ 1,
  data$check_pop== "Racial minorities will outnumber Whites in the U.S. by the year 2046." ~ 1,
  data$check_geo== "Recent data reveal more people moved over the last year." ~ 1,
  data$check_culture=="Racial minorities will be the majority of leads in TV shows and movies by the year 2032." ~ 1,
  TRUE ~ 0
)

### Recode 5 pt Unlikey-Likely Scale Questions ###
likely5_recode<- function (x) {
  case_when(
    x== "Extremely unlikely" ~ 0,
    x== "Somewhat unlikely" ~ 1/4,
    x== "Neither likely nor unlikely" ~ 2/4,
    x== "Somewhat likely" ~ 3/4,
    x== "Extremely likely" ~ 1,
    NA ~ NA_real_
  )
}

data<-data%>%
  mutate_at(c("trends_closed_demog", "trends_closed_pol", "trends_closed_media"), likely5_recode)

### Recode 7 pt Agree-Disagree Scale Questions ###
agree7_recode<- function (x) {
  case_when(
    x== "Strongly disagree" ~ 0,
    x== "Disagree" ~ 1/6,
    x== "Somewhat disagree" ~ 2/6,
    x== "Neither agree nor disagree" ~ 3/6,
    x== "Somewhat agree" ~ 4/6,
    x== "Agree" ~ 5/6,
    x== "Strongly agree" ~ 1,
    NA ~ NA_real_
  )
}

data<-data%>%
  mutate_at(c("conservative_ideo", "SC_1", "SC_3", "ST_1", "ST_3", 
              "PT_1", "PT_2", "RT_1", "RT_3"), agree7_recode)

agree7_recode_rev<- function (x) {
  case_when(
    x== "Strongly disagree" ~ 1,
    x== "Disagree" ~ 5/6,
    x== "Somewhat disagree" ~ 4/6,
    x== "Neither agree nor disagree" ~ 3/6,
    x== "Somewhat agree" ~ 2/6,
    x== "Agree" ~ 1/6,
    x== "Strongly agree" ~ 0,
    NA ~ NA_real_
  )
} #This is recoded reverse coded items

data<-data%>%
  mutate_at(c("liberal_ideo", "SC_2", "ST_2", "PT_3", "RT_2"), agree7_recode_rev)

### Recode FTs Questions ###
data<-data%>%
  mutate(Trump_FT= Trump_FT/10, Biden_FT= Biden_FT/10, Swift_FT= Swift_FT/10, Kimmel_FT= Kimmel_FT/10,
         Asian_FT= Asian_FT/100, Black_FT= Black_FT/100, Latinos_FT= Latinos_FT/100, White_FT=White_FT/100,
         Alt_Right_FT=Alt_Right_FT/100, BLM_FT=BLM_FT/100)

### Recode 7 pt Likey-Unlikely Scale Questions ###
likely7_recode<- function (x) {
  case_when(
    x== "Extremely unlikely" ~ 0,
    x== "Moderately unlikely" ~ 1/6,
    x== "Slightly unlikely" ~ 2/6,
    x== "Neither likely nor unlikely" ~ 3/6,
    x== "Slightly likely" ~ 4/6,
    x== "Moderately likely" ~ 5/6,
    x== "Extremely likely" ~ 1,
    NA ~ NA_real_
  )
}

data<-data%>%
  mutate_at(c("Sign_Racist_Cand", "White_History_SchoolBoard", "Vote_Antiracist", "Rally_for_Inc_Immi",
              "Buy_Poor_Whites_Book", "Boycott_Undoc_Immi_Labor", "Buy_POC_Book", "Buy_Help_Undoc_Immi"), likely7_recode)


### Recode 7 pt Oppose-Favor Scale Questions ###
oppose7_recode<- function (x) {
  case_when(
    x== "Strongly Oppose" ~ 0,
    x== "Oppose" ~ 1/6,
    x== "Somewhat Oppose" ~ 2/6,
    x== "Neither Favor Nor Oppose" ~ 3/6,
    x== "Somewhat Favor" ~ 4/6,
    x== "Favor" ~ 5/6,
    x== "Strongly Favor" ~ 1,
    NA ~ NA_real_
  )
}

data<-data%>%
  mutate_at(c("Legacy_Admit", "Inc_Border_Pat", "No_Trans_Bathroom"), oppose7_recode)


oppose7_recode_rev<- function (x) {
  case_when(
    x== "Strongly Oppose" ~ 1,
    x== "Oppose" ~ 5/6,
    x== "Somewhat Oppose" ~ 4/6,
    x== "Neither Favor Nor Oppose" ~ 3/6,
    x== "Somewhat Favor" ~ 2/6,
    x== "Favor" ~ 1/6,
    x== "Strongly Favor" ~ 0,
    NA ~ NA_real_
  )
} #This is recoded reverse coded items

data<-data%>%
  mutate_at(c("Expand_Immi", "Inc_Tax_Wealthy", "Abortion_Access"), oppose7_recode_rev)


### Calculating Chronbach's Alpha for Constructs with Multiple Items ###
library(psych)
policy_vars<-data[, c("Legacy_Admit", "Expand_Immi", "Inc_Border_Pat", "Inc_Tax_Wealthy",
                      "No_Trans_Bathroom", "Abortion_Access")]
alpha(policy_vars, check.keys= T)

status_change_vars<-data[, c("SC_1", "SC_2", "SC_3")]
alpha(status_change_vars, check.keys= T)

status_threat_vars<-data[, c("ST_1", "ST_2", "ST_3")]
alpha(status_threat_vars, check.keys= T)

prototypicality_threat_vars<-data[, c("PT_1", "PT_2", "PT_3")]
alpha(prototypicality_threat_vars, check.keys= T)

realistic_threat_vars<-data[, c("RT_1", "RT_2", "RT_3")]
alpha(realistic_threat_vars, check.keys= T)

general_threat_vars<-data[, c("SC_1", "SC_2", "SC_3", "ST_1", "ST_2", "ST_3",
                             "PT_1", "PT_2", "PT_3", "RT_1", "RT_2", "RT_3")]
alpha(general_threat_vars, check.keys= T)

### Creating Scales for Constructs with Multiple Items ###
data<-data%>%
  mutate(SC_scale= (SC_1+SC_3+SC_2)/3, ST_scale= (ST_1+ST_3+ST_2)/3,
         RT_scale= (RT_1+RT_3+RT_2)/3, PT_scale= (PT_1+PT_2)/2, PT_scale_full= (PT_1+PT_2+PT_3)/3,
         general_threat_scale= (RT_1+RT_3+RT_2+SC_1+SC_3+SC_2+ ST_1+ST_3+ST_2+PT_1+PT_2+PT_3)/12,
         policy_scale= (Legacy_Admit+ Expand_Immi+ Inc_Border_Pat+ Inc_Tax_Wealthy+
         No_Trans_Bathroom+ Abortion_Access)/6,
         racialpolicy_scale= (Legacy_Admit+ Expand_Immi+ Inc_Border_Pat)/3,
         nonracialpolicy_scale= (Inc_Tax_Wealthy+ No_Trans_Bathroom+ Abortion_Access)/3,
         cons_ideo_scale= (liberal_ideo+ conservative_ideo)/2,
         backlash_pol= (Sign_Racist_Cand+ White_History_SchoolBoard)/2, 
         backlash_person= (Buy_Poor_Whites_Book+ Boycott_Undoc_Immi_Labor)/2, 
         support_pol= (Vote_Antiracist+ Rally_for_Inc_Immi)/2, 
         support_person= (Buy_POC_Book+ Buy_Help_Undoc_Immi)/2)

### Loading in Demographic Data and Merging with Qualtrics Survey Data ###
demo_data<-read.csv("01_Study Demographic Data.csv")

full_data<-merge(demo_data, data, by.x= "id", by.y= "RESPONDENT_ID")

### Recoding Partisanship to Remove NAs ###
full_data$political_party_preference<-case_when(
  full_data$political_party_preference>7 ~ NA_real_,
  TRUE ~ full_data$political_party_preference
)

### Exporting Clean Data ###
write.csv(full_data, file= "01_Clean Racial Shift Replication.csv", row.names = F)
